package com.estronger.shopmanage.adapter.order;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.ldfxEntity;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PrefUtils;
import com.estronger.shopmanage.utils.ScreenUtils;
import com.estronger.shopmanage.view.VerticalProgressBar;

import java.text.DecimalFormat;
import java.util.List;


public class StatisticalAdpter extends RecyclerView.Adapter<StatisticalAdpter.ViewHolder> implements View.OnClickListener {

    private List<ldfxEntity> mList;
    Context mContext;
    DecimalFormat df = new DecimalFormat("######0");
    DecimalFormat df2 = new DecimalFormat("######0.00");

    public StatisticalAdpter(Context context, List<ldfxEntity> lt) {
        mContext = context;
        mList = lt;
    }

    /**
     * .     * Item的回调接口
     * .     *
     * .
     */
    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    private OnItemClickListener listener; // 点击Item的回调对象

    /**
     * .     * 设置回调监听
     * .     *
     * .     * @param listener
     * .
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvname;
        public TextView tvvalue,tvvalue2;
        public VerticalProgressBar vpbar;
        public RelativeLayout pbbarrl;

        // TODO Auto-generated method stub
        public ViewHolder(View v) {
            super(v);
        }
    }

    /**
     * .     * 获取总的条目数量
     * .
     */
    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    /**
     * .     * 创建ViewHolder
     * .
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // TODO Auto-generated method stub
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adpter_statistical, parent, false);
        ViewHolder holder = new ViewHolder(v);
        holder.tvname = (TextView) v.findViewById(R.id.tvname);
        holder.tvvalue = (TextView) v.findViewById(R.id.tvvalue);
        holder.tvvalue2 = (TextView)v.findViewById(R.id.tvvalue2);
        holder.vpbar = (VerticalProgressBar) v.findViewById(R.id.progressbar);
        holder.pbbarrl = (RelativeLayout) v.findViewById(R.id.pbbarrl);
        v.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            //注意这里使用getTag方法获取position
            listener.onItemClickListener(v, (int) v.getTag());
        }

    }

    /**
     * .     * 将数据绑定到ViewHolder上
     * .
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        // TODO Auto-generated method stub
        try {
            holder.pbbarrl.setTag(position);

            //给控件赋值
            String name = ConvertUtils.formatNull(mList.get(position).getName());
            holder.tvname.setText(name.length() > 12 ? name.substring(0, 12) + "..." : name);
            holder.tvvalue.setText(ConvertUtils.formatNull(mList.get(position).getNum()));
            holder.tvvalue2.setText(ConvertUtils.formatNull(mList.get(position).getNum()));

            //当是0时就没什么好算的
            if (mList.get(position).getNum().equals("0")) {
                holder.vpbar.setProgress(0);
                holder.vpbar.setBackgroundColor(Color.parseColor(mList.get(position).getColor()));
                ViewGroup.LayoutParams params = holder.vpbar.getLayoutParams();
                params.width = 0;
                holder.vpbar.setLayoutParams(params);
            } else {
                //获取第一条全部的长度
                if (position == 0) {
                    //根据数量的长度来衰减
                    ViewGroup.LayoutParams valueParams = holder.tvvalue.getLayoutParams();
                    valueParams.width = holder.tvvalue.length() * 10;
                    holder.tvvalue.setLayoutParams(valueParams);

                    ViewGroup.LayoutParams params = holder.vpbar.getLayoutParams();
                    params.width = ScreenUtils.getScreenWidth(mContext) - ScreenUtils.dpToPx(mContext, valueParams.width + 170);
                    holder.vpbar.setLayoutParams(params);
                    PrefUtils.setString(mContext, "firstWidth", String.valueOf(params.width));
                } else {
                    int firstWidth = Integer.valueOf(PrefUtils.getString(mContext, "firstWidth", "0"));

                    ViewGroup.LayoutParams params = holder.vpbar.getLayoutParams();
                    params.width = Integer.valueOf(df.format(firstWidth * Double.valueOf(String.valueOf(df2.format((Double.valueOf(mList.get(position).getNum()) / Double.valueOf(mList.get(0).getNum())))))));
                    holder.vpbar.setLayoutParams(params);
                }

                int process = Integer.parseInt(df.format(((Double.valueOf(mList.get(position).getNum()) / Double.valueOf(mList.get(0).getNum())) * 100)));
                holder.vpbar.setProgress(process);
                holder.vpbar.setBackgroundColor(Color.parseColor(mList.get(position).getColor()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
